package com.hymall.service.admin;

import com.github.pagehelper.PageInfo;
import com.hymall.bean.MarketIssue;
import com.hymall.bean.MarketIssueExample;
import com.hymall.bean.common.BasePageInfo;
import com.hymall.bean.common.CommonData;
import com.hymall.mapper.MarketIssueMapper;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: 牟治宇
 * @description 通用问题实现类
 * @date: 2022/7/18 12:28
 */

@Service
public class IssueServiceImpl implements IssueService {
    @Autowired
    MarketIssueMapper marketIssueMapper;

    @Override
    public MarketIssue insertIssue(String question, String answer) {

        MarketIssue marketIssue = new MarketIssue();

        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);

        try {
            Date parse = simpleDateFormat.parse(format);

            marketIssue.setQuestion(question);
            marketIssue.setAnswer(answer);
            marketIssue.setAddTime(parse);
            marketIssue.setUpdateTime(parse);
            marketIssue.setDeleted(false);
        } catch (ParseException e) {
            e.printStackTrace();
        }


        int code = marketIssueMapper.insert(marketIssue);

        marketIssue.setId(marketIssue.getId());
        return marketIssue;
        // BeanUtils.copyProperties();
        // marketIssu
        // // marketIssueMapper.insertSelective(question,answer);

    }

    @Override
    public int updateIssue(MarketIssue marketIssue) {
        marketIssue.setUpdateTime(new Date());
        int code = marketIssueMapper.updateByPrimaryKey(marketIssue);
        return code;
    }

    @Override
    public int deleteIssue(MarketIssue marketIssue) {

        Integer id = marketIssue.getId();
        int code = marketIssueMapper.issueDelete(id);
        return code;
    }

    @Override
    public CommonData issueList(BasePageInfo basePageInfo, String question) {
        MarketIssueExample marketIssueExample = new MarketIssueExample();
        if (question != null) {
            marketIssueExample.createCriteria().andQuestionLike("%" + question + "%");
        }
        marketIssueExample.createCriteria().andDeletedEqualTo(false);
        List<MarketIssue> marketIssues = marketIssueMapper.selectByExample(marketIssueExample);
        PageInfo<MarketIssue> marketIssuePageInfo = new PageInfo<>(marketIssues);

        return CommonData.data(marketIssuePageInfo);
    }


}
